package org.example.course.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import lombok.extern.slf4j.Slf4j;
import org.example.commonapi.domain.ChoiceOptions;
import org.example.commonapi.domain.R;
import org.example.commonapi.domain.TaskQuestionnaireQuestions;
import org.example.commonapi.util.options.ChoiceOptionsService;
import org.example.course.service.TaskQuestionnaireQuestionsService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/course")
@Slf4j
public class TaskQuestionnaireController {

    @Resource
    private TaskQuestionnaireQuestionsService taskQuestionnaireService;

    @Resource
    private ChoiceOptionsService choiceOptionsService;

    /**
     * 根据任务id获取问卷
     * @param taskId
     * @return
     */
    @GetMapping("/list/task/questionnaire/{taskId}")
    public R listTaskQuestionnaire(@PathVariable Long taskId){
        LambdaQueryWrapper<TaskQuestionnaireQuestions> queryWrapper=new LambdaQueryWrapper<>();
        queryWrapper.eq(TaskQuestionnaireQuestions::getTaskId,taskId);
        List<TaskQuestionnaireQuestions> list=taskQuestionnaireService.list(queryWrapper);
        return R.success(list);
    }

    /**
     * 根据任务id插入问卷
     * @param id
     * @param taskQuestionnaireQuestions
     * @return
     */
    @PostMapping("/insert/task/questionnaire/{id}")
    public R insertTaskQuestionnaire(@PathVariable Long id,@RequestBody List<TaskQuestionnaireQuestions> taskQuestionnaireQuestions){
        for(TaskQuestionnaireQuestions taskQuestionnaireQuestion:taskQuestionnaireQuestions){
            taskQuestionnaireQuestion.setTaskId(id);
        }
        taskQuestionnaireService.saveBatch(taskQuestionnaireQuestions);
        List<Long> ids=taskQuestionnaireQuestions.stream().map(TaskQuestionnaireQuestions::getId).collect(Collectors.toList());
        return R.success(ids);
    }

    /**
     * 插入选项
     * @param choiceOptions
     * @return
     */
    @PostMapping("/insert/task/questionnaire/options")
    public R insertTaskQuestionnaireChoice(@RequestBody List<ChoiceOptions> choiceOptions){
        choiceOptionsService.saveBatch(choiceOptions);
        return R.success();
    }

    /**
     * 根据问卷id获取选项
     * @param id
     * @return
     */
    @GetMapping("/get/task/questionnaire/options/{id}")
    public R getTaskQuestionnaireChoice(@PathVariable Long id){
        LambdaQueryWrapper<ChoiceOptions> queryWrapper=new LambdaQueryWrapper<>();
        queryWrapper.eq(ChoiceOptions::getTaskQuestionnaireId,id);
        ChoiceOptions choiceOptions=choiceOptionsService.getOne(queryWrapper);
        return R.success(choiceOptions);
    }

}
